package com.ujs.edu.medicine.controller;


import com.auth0.jwt.JWT;
import com.ujs.edu.medicine.dao.YunjiayiAddressMapper;
import com.ujs.edu.medicine.dao.YunjiayiUserMapper;
import com.ujs.edu.medicine.dataobject.YunjiayiAddress;
import com.ujs.edu.medicine.dataobject.YunjiayiUser;
import com.ujs.edu.medicine.util.Result;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@RestController
public class AddressController {
    @Autowired
    private YunjiayiAddressMapper yunjiayiAddressMapper;
    @Autowired
    private YunjiayiUserMapper yunjiayiUserMapper;
    @Autowired
    private HttpServletRequest request;

    private String gettoken() {
        return request.getHeader("Authorization");
    }

    /*获取地址列表*/
    @GetMapping(path = "/api/address/list")
    public Result updateUser() {
        Result result = new Result();
        String token = gettoken();
        int userId =0;
        try {
            userId=JWT.decode(token).getClaim("id").asInt();
        }catch (Exception e){
            result.setCode(1);
            result.setMessage("您还没有登陆，请登录");
            return  result;
        }

        YunjiayiUser yunjiayiUser = yunjiayiUserMapper.selectByPrimaryKey(userId);
        String name = yunjiayiUser.getRealname();
        List<YunjiayiAddress> address = yunjiayiAddressMapper.selectAddressByuId(userId);
        JSONObject[] JSONObjectArry = new JSONObject[address.size()];
        for (int i = 0; i < address.size(); i++) {
            JSONObjectArry[i] = new JSONObject();
            System.out.println(address.get(i).getAddressId());
            JSONObjectArry[i].put("id", address.get(i).getAddressId());
            JSONObjectArry[i].put("value", address.get(i).getAddress());
            JSONObjectArry[i].put("name", address.get(i).getNote());
            JSONObjectArry[i].put("number", address.get(i).getTelephone());
            JSONObjectArry[i].put("ismain", address.get(i).getDefaultaddress());
            JSONObjectArry[i].put("ordernumber", i);
        }
        result.setCode(address != null ? 1 : 0);
        result.setMessage(address != null ? "获取成功" : "获取失败,无数据记录");
        result.setData(address != null ? JSONObjectArry : null);
        return result;
    }


    /*修改收货地址*/
    @PutMapping(path = "/api/address")
    public Result Putaddress(@RequestBody Map<String,String>map) {
        Result result = new Result();

        String token = gettoken();
        int userId =0;
        try {
            userId=JWT.decode(token).getClaim("id").asInt();
        }catch (Exception e){
            result.setCode(1);
            result.setMessage("您还没有登陆请登陆");
            return result;
        }

        YunjiayiUser user1 = yunjiayiUserMapper.selectByPrimaryKey(1);
        YunjiayiAddress address =yunjiayiAddressMapper.selectByPrimaryKey(Integer.parseInt(map.get("id")));

        if (address!=null){
            address.setNote(map.get("name") != null ? map.get("name") : user1.getRealname());
            address.setTelephone(map.get("number") != null ? map.get("number") : user1.getPhonenumber());
            address.setAddress(map.get("value"));
            address.setAduserid(1);
            address.setDefaultaddress(false);

        }

        int tip = yunjiayiAddressMapper.updateByPrimaryKey(address);

        result.setCode(tip);
        result.setMessage(tip != 0 ? "修改成功" : "修改失败");
        return result;
    }


    /*删除收获地址*/
    @DeleteMapping(path = "/api/address")
    public Result Deleteaddress(@RequestBody Map<String,String>map) {
        Result result = new Result();

        String token=gettoken();
        int myuserid=0;
        try {
            myuserid=JWT.decode(token).getClaim("id").asInt();
        }catch (Exception e){
            result.setCode(1);
            result.setMessage("您还没有登陆，请登陆");
            return result;
        }
        int tip = yunjiayiAddressMapper.deleteByPrimaryKey(Integer.parseInt(map.get("id")));
        result.setCode(tip);
        result.setMessage(tip != 0 ? "删除成功" : "删除失败");
        return result;
    }


    /*添加收获地址*/
    @PostMapping(path = "/api/address")
    public Result Postaddress(@RequestBody Map<String ,String>map) {
        Result result = new Result();
        String token = gettoken();
        int myuserid=0;
        try {
            myuserid=JWT.decode(token).getClaim("id").asInt();
        }catch (Exception e){
            result.setCode(1);
            result.setMessage("您还没有登陆，请登陆");
            return result;
        }

        YunjiayiUser user1 = yunjiayiUserMapper.selectByPrimaryKey(myuserid);
        YunjiayiAddress address = new YunjiayiAddress();
        address.setAddress(map.get("value"));
        address.setAduserid(1);
        address.setDefaultaddress(false);
        address.setNote(map.get("name") != null ? map.get("name") : user1.getRealname());
        address.setTelephone(map.get("number") != null ? map.get("number") : user1.getPhonenumber());
        int tip = yunjiayiAddressMapper.insert(address);
        result.setCode(tip);
        result.setMessage(tip != 0 ? "增加成功" : "增加失败");
        return result;
    }
}